package com.qf.java2201.dao.impl;

import com.qf.java2201.dao.OrderDao;
import com.qf.java2201.pojo.HOrder;
import com.qf.java2201.utils.DateUtil;
import com.qf.java2201.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @date 2022/4/9
 * @desc
 */
public class OrderDaoImpl implements OrderDao {

    QueryRunner queryRunner = new QueryRunner(DbUtils.getDs());

    @Override
    public int getPayOrderCount(String odate, String edid, String sjdid, int yyztid) {
        //查询  当前  已付款的预约单 个数
        String sql = "select count(*) from h_order where edid = ? and odate = ? and sjdid = ? and yyztid = ?";

        Long query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Long>(), edid, odate, sjdid, yyztid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return query.intValue();
    }

    @Override
    public int addOrder(HOrder hOrder) {
        String sql = "insert into h_order values (null,?,?,?,?,?,?,?,now())";


        BigInteger oid = null;
        try {
            int update = queryRunner.update(sql, hOrder.getEdid(), hOrder.getUdid(), hOrder.getSjdid(), hOrder.getYyztid(),
                    hOrder.getSickness(), hOrder.getGhf(), hOrder.getOdate());

            //查询刚才插入的数据的主键值
            String sql2 = "select LAST_INSERT_ID()";
            oid = queryRunner.query(sql2, new ScalarHandler<BigInteger>());   //  结果集中 是一个数  使用ScalarHandler 来进行转换
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return oid.intValue();
    }

    @Override
    public int updateOrderStatus(String oid, Integer yyztid) {
        int update = 0;
        String sql = "update h_order set yyztid = ? where oid = ?";

        if(oid==null){
            //定时器执行任务  把所有未付款的单子  都给取消调
            sql =  "update h_order set yyztid = ? where yyztid = 21 ";

            try {
                update = queryRunner.update(sql, yyztid);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return update;

        }else{
            try {
                update = queryRunner.update(sql, yyztid, oid);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return update;

        }


    }


    // 查询患者的 预约单列表
    @Override
    public List<HOrder> findOrderByUdid(Integer did)  {

        String sql = "SELECT e.drealname AS ename  , o.sickness , o.odate , t1.tname AS sjd , o.ghf, t2.tname  AS yyzt ,o.yyztid , o.oid FROM h_order o JOIN h_userinfo u ON o.udid = u.did \n" +
                "JOIN h_type t1 ON o.sjdid = t1.tid    " +    // t1  当成时间段表
                "JOIN h_type t2 ON o.yyztid = t2.tid   " +    // t2 预约单状态表
                "\n" +
                "JOIN h_userinfo e ON o.edid = e.did    " +   //  把e 当成 医生表
                "WHERE u.`did` = ? " ;

        List<HOrder> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<>(HOrder.class), did);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        return query;
    }


    // 查询 某个预约日期的  当天的 取消次数
    @Override
    public int countDayQxCount(String oid, Integer did, Date odate) {

        String sql = "select count(*) from h_order where udid = ? and yyztid = 23 and odate = ?";

        Long qxCount = null;
        try {
            qxCount = queryRunner.query(sql, new ScalarHandler<Long>(), did,odate);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return qxCount.intValue();
    }



    // 统计  oid 预约单所在周的 取消次数
    @Override
    public int countWeekQxCount(String oid, Integer did, Date odate) {

        Calendar instance = Calendar.getInstance();
        instance.setTime(odate);

        // 把日历 翻到 这个日期所在周的 周一
        instance.set(Calendar.DATE,instance.get(Calendar.DATE)-(instance.get(Calendar.DAY_OF_WEEK)- 2));
        // 周一的日期
        Date zy = instance.getTime();
        // 把日历 翻到 这个日期所在周的 周五
        instance.set(Calendar.DATE,instance.get(Calendar.DATE)-(instance.get(Calendar.DAY_OF_WEEK)- 2) + 4);
        // 周五的日期
        Date zw = instance.getTime();

        String sql = "SELECT COUNT(*) FROM h_order WHERE udid = ? AND yyztid = 23 AND odate <= ? AND odate >=? ";

        Long query = null;
        try {
            query = queryRunner.query(sql, new ScalarHandler<Long>(), did, zw, zy);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query.intValue();
    }


    public static void main(String[] args) {
        // 得到 某个日期的 周一 和 周五的日期

        Date date = DateUtil.strToDate("2022-04-13", "yyyy-MM-dd");

        Calendar instance = Calendar.getInstance();
        instance.setTime(date);


        // 把日历 翻到 这个日期所在周的 周一
        instance.set(Calendar.DATE,instance.get(Calendar.DATE)-(instance.get(Calendar.DAY_OF_WEEK)- 2));

        Date zy = instance.getTime();
        //System.out.println(instance.get(Calendar.DATE));   // 19
        //System.out.println(instance.get(Calendar.DAY_OF_WEEK) - 2);

        // 把日历 翻到 这个日期所在周的 周五
        instance.set(Calendar.DATE,instance.get(Calendar.DATE)-(instance.get(Calendar.DAY_OF_WEEK)- 2) + 4);
        Date zw = instance.getTime();
        System.out.println(zy);
        System.out.println(zw);
    }
}
